package com.twotiger.and.view;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

public class CalendarObj {
    
    private Map<Integer, int[]> gregorianCreated = new HashMap<Integer, int[]>();

    public CalendarObj(int index, int year, int month) {
        super();
        gregorianCreated.put(index, buildGregorian(year, month));
    }
    
    public Map<Integer, int[]> getGregorianCreated() {
        return gregorianCreated;
    }

    public int[] buildGregorian(int year, int month) {

        int[] date = new int[44];
        
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int monthStart = dayInWeek;
        if (monthStart == 1) {
            monthStart = 8;
        }
        monthStart -= 2; ///以日为开头-1，以星期一为开头-2
        date[monthStart] = 1;
        
      //获取当月的天数   （先加一个月设为下个月，然后调用set0 get得到这个月最后一天）
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
        
        for (int i = 1; i < monthDay; i++) {
            date[monthStart + i] = i + 1;
        }
        date[42] = year;
        date[43] = month;
        return date;
    }
    
    public boolean isShowMonth(int index, int downIndex){
        return gregorianCreated.get(index)[downIndex] != 0;
    }
    public Date getDownDate(int index, int downIndex){
        int[] info = gregorianCreated.get(index);
        int year = info[42];
        int month = info[43];
        int day = 0;
        if(downIndex == -1){
            day = 1;
        }else {
            day = info[downIndex];
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return calendar.getTime();
    }
    public int getStartIndex(int index){
        int[] info = gregorianCreated.get(index);
        int startIndex = 0;
        for (int i = 0; i < info.length - 2; i++) {
            if(1 == info[i]){
                startIndex = i;
                break;
            }
        }
        return startIndex;
    }
}
